define(function (require, exports, module) {
    require("apps/lab/page.css");
    require("apps/easyui");
    require("fancybox/jquery.fancybox.css");
    require("fancybox/jquery.fancybox");
    var comp = require("apps/lab/components");

    exports.init = function(ctx, uuid, config){
        var page = new $.PageDomFactory(uuid);
        var tabs = page.getJqDom("tabs");

        var rendered = false;
        tabs.tabs({
            onSelect:function(title){
                if(!rendered && title == "实验数据"){
                    rendered = true;
                    doGetParseJson(config.rootPath, config.hasChart);
                }
            }
        });

        page.getJqDom("processValue").each(function(){
            var type = $(this).attr("data-field-type");
            comp.render(ctx, type, $(this));
        });
        var template = '{{#.}}<dl class="grant-img"><dt>' +
            '<a data-id="facy" href="'+ ctx +'/print/{{path}}" title="{{title}}">' +
            '<img style="width: 120px;height: 80px;" alt="" src="'+ ctx +'/print/{{path}}" /></a>' +
            '</dt><dd><span class="title">{{_title}}</span></dd>' +
            '</dl>{{/.}}';

        function renderLine(dom, json){
            var ec = require("echarts/echarts");
            var themes = require("echarts/theme/gray");
            require("zrender/zrender");
            var serieData = [], titles = [], times = json.times;
            for(var i in json.data){
                serieData.push({
                    name:i,
                    type:"line",
                    selected:false,
                    data:json.data[i]
                });
                titles.push({
                    name : i,
                    selected:false,
                    textStyle : {
                        fontSize:12
                    }
                });
            }
            var option = {
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient:"horizontal",
                    data:titles
                },
                dataZoom: {
                    show: true,
                    start : 0,
                    end:100,
                    y:460
                },
                grid:{
                    y:120,
                    y2:80
                },
                calculable : false,
                xAxis : [
                    {
                        type : 'category',
                        boundaryGap : false,
                        data : times
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : serieData
            };

            var myChart = ec.init(dom);
            myChart.setOption(option);
        }

        function doGetParseJson(rootPath, hasChart) {
            $("body").wait("start", "数据加载中，等待图形渲染！");
            $.getJSON(ctx + "/print/" + rootPath + "/data.json", function(data){
                if ($.isPlainObject(data) && data.error) {
                    $.messager.alert('信息',
                        data.msg, 'info');
                } else {
                    var labels = data.labels, labelHtm = [];
                    if(labels && labels.length){
                        var labelHolder = $("<div style='margin-bottom: 10px;'></div>").appendTo(page.getJqDom("data"));
                        for(var i = 0, l = labels.length; i < l; i++){
                            if(labels[i].type == "LABEL_TYPE_IMG"){
                                var uuid = $.utils.createUUID(6);
                                var path = ctx + "/print" + labels[i].text;
                                labelHtm.push('<a class="ml10" data-id="viewimg" href="'+ path +'"><img src="'+ path + '" style="width: 60px;height: 30px;"/></a>');
                            }else{
                                labelHtm.push("<span class='ml10'>" + labels[i].text + "<span>");
                            }
                        }
                        labelHolder.html(labelHtm.join("")).children("a[data-id=viewimg]").each(function(){
                            $(this).fancybox();
                        });
                    }
                    if(hasChart == '1'){
                        var l = data.times.length * 10, ll = 0, fy = 0;
                        for (var i in data.data){
                            ll++;
                        }
                        ll = (ll / 4) * 200;
                        l = l < 1400 ? 1400 : l;
                        l = l > 4000 ? 4000 : l;
                        l = l < ll ? ll : l;
                        var id = $.utils.createUUID(6);
                        page.getJqDom("data").append("<a data-id='view' class='' href='#"+ id +"' iconCls='fa fa-eye fa-lg'>查看实验数据图</a><div style='display: none;'><div style='overflow-x: auto;overflow-y:hidden'>" +
                            "<div id='"+ id +"'></div></div></div>").find("a[data-id=view]").linkbutton().eq(0).fancybox({
                            fitToView:true
                        });
                        $("#" + id).css({
                            width:l,
                            height:500
                        });
                        renderLine(document.getElementById(id), data, fy);
                    }
                    $("<a data-id='download' " +
                        "href=\""+ ctx +"/download/"+ rootPath +"/data.xls\" " +
                        "class='ml10' href='#"+ id +"' iconCls='fa fa-download fa-lg'>下载实验数据</a>").appendTo(page.getJqDom("data")).linkbutton();
                    $("body").wait("end");
                }
            }, "JSON");
        }
    }
});